package com.tech.connect.zhaofuwu;

import android.content.Intent;
import android.graphics.SurfaceTexture;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ksy.common.image.ImageLoader;
import com.ksy.common.utils.DateUtil;
import com.tech.connect.R;
import com.tech.connect.activity.BaseActivity;
import com.tech.connect.api.BaseEntityOb;
import com.tech.connect.api.HttpUtils;
import com.tech.connect.model.ItemQuznZi;

import java.io.IOException;
import java.io.Serializable;
import java.util.Map;

public class QuanZiVideoDetailActivity extends BaseActivity {


    private String mVideoUrl;

    private MediaPlayer player;
    private TextureView mTextureView;
    private Surface surface;
    private ItemQuznZi item;
    private ImageView ivCover;
    private ImageView ivPlay;
    private ImageView ivHead;
    private TextView tvName;
    private TextView tvContent;
    private TextView tvDate;
    private TextView comment;
    private TextView yuedan;
    private EditText input;
    private View llInput;
    private TextView tvPraise;
    private TextView tvFavorite;
    private TextView tvChat;

    @Override
    protected boolean isShowHeadBar() {
        return true;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quanzi_video_detail);
        getHeadBar().setTitle("服务圈视频");
        Intent intent = getIntent();
        Serializable data = intent.getSerializableExtra("data");
        if (data == null || (!(data instanceof ItemQuznZi))) {
            showToast("数据不可用");
            destroyActivity();
            return;
        }
        item = (ItemQuznZi) data;
        initView();
        ImageLoader.getInstance().load(activity, item.videoCover, ivCover);
        String headUrl = "";
        String name = "";
        if (item.obj != null && item.obj.user != null) {
            headUrl = item.obj.user.headImage;
            name = item.obj.user.nickName;
        }
        ImageLoader.getInstance().load(activity, headUrl, ivHead);
        tvName.setText(name);
        if (isEmpty(item.content)) {
            tvContent.setVisibility(View.GONE);
        } else {
            tvContent.setVisibility(View.VISIBLE);
        }
        tvContent.setText(item.content);
        tvDate.setText(DateUtil.getSmartDate(item.createTime));
        mVideoUrl = item.videos;
        if (item.obj != null) {
            tvPraise.setText(item.obj.commentCount);
            tvFavorite.setText(item.obj.likeCount);
        } else {
            tvPraise.setText("0");
            tvFavorite.setText("0");
        }
    }


    private void initView() {
        llInput = findViewById(R.id.llInput);
        tvPraise = findViewById(R.id.tvPraise);
        tvFavorite = findViewById(R.id.tvFavorite);
        tvChat = findViewById(R.id.tvChat);
        tvChat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //
            }
        });
        input = findViewById(R.id.input);
        comment = findViewById(R.id.comment);
        comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String trim = input.getText().toString().trim();
                if (isEmpty(trim)) {
                    showToast("请输入内容");
                    return;
                }
                comment(trim, item);
            }
        });
        yuedan = findViewById(R.id.yuedan);
        yuedan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                jump2Activity(QuanZiYueDanListActivity.class);
            }
        });
        ivCover = findViewById(R.id.ivCover);
        ivPlay = findViewById(R.id.ivPlay);
        ivPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ivCover.setVisibility(View.GONE);
                if (player == null) {
                    return;
                }
                if (player.isPlaying()) {
                    player.pause();
                } else {
                    player.start();
                }
            }
        });
        ivHead = findViewById(R.id.ivHead);
        tvName = findViewById(R.id.tvName);
        tvContent = findViewById(R.id.tvContent);
        tvDate = findViewById(R.id.tvDate);
        mTextureView = findViewById(R.id.play_video_TextureView);
        mTextureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
                surface = new Surface(surfaceTexture);
                player = new MediaPlayer();

                player.setAudioStreamType(AudioManager.STREAM_MUSIC);
                //设置显示视频显示在SurfaceView上
                player.setSurface(surface);
                try {
                    player.setDataSource(mVideoUrl);
                    player.prepare();
                    player.setLooping(true);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width, int height) {

            }

            @Override
            public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
                stop();
                release();
                return true;
            }

            @Override
            public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {

            }
        });

    }

    private void comment(String data, final ItemQuznZi item) {
        Map<String, Object> map = HttpUtils.getMap();
        map.put("content", data);
        map.put("typeId", item.id);
        map.put("type", 50);
        HttpUtils.comment(map, new BaseEntityOb<Object>() {
            @Override
            public void onStart() {
                super.onStart();
                showDialog();
            }

            @Override
            public void onDataDeal(boolean isSuccess, Object data, String msg) {
                hideDialog();
                showToast(msg);
                if (isSuccess) {
                    input.setText("");
                }
//                if (item.obj == null) {
//                    item.obj = new ItemQuznZi.Extra();
//                }
//                item.obj.likeCount = data;
//                adapter.notifyItemChanged(position);
            }
        });
    }


    @Override
    public void onPause() {
        super.onPause();
        stop();
        release();
    }

    /**
     * 停止播放
     */
    private void stop() {
        if (player == null || player.isPlaying()) {
            return;
        }
        player.stop();
    }

    /**
     * 释放资源
     */
    private void release() {
        if (player == null) {
            return;
        }
        player.release();
        player = null;
        surface = null;
    }

}